package com.aoyy.dao;

import java.util.List;
import org.springframework.stereotype.Repository;
import com.aoyy.model.User;
import com.aoyy.constant.Constants;
/**=====================================================================================
 * DAO class <code>UserDao</code>
 * @author this author
 * @date 2017-08-06
 * ====================================================================================== */
@Repository
public class UserDao extends BaseDao<User> {
	// Query user by user name
	public User queryUserByUserName(String userName) {
		String hql = "from User where userName = '" + userName + "'";
		@SuppressWarnings("rawtypes")
		List list = this.find(hql);
		if (list != null && !list.isEmpty()) {
			return (User) list.get(0);
		}
		return null;
	}
	
	public User queryUserByUserId(int userId) {
		String hql = "from User where userId = " + userId;
		@SuppressWarnings("rawtypes")
		List list = this.find(hql);
		if (list != null && !list.isEmpty()) {
			return (User) list.get(0);
		}
		return null;
	}
	
	// Query all users 
	public List<User> queryAllUsers() {
		String hql = "from User order by lastVisit desc";
		@SuppressWarnings("unchecked")
		List<User> list = this.find(hql);
		return list;
	}
	
	public void lockUser(String userName) {
		String hql = "update User set locked = " + Constants.USER_LOCKED + " where userName = '" + userName + "'";
		this.executeUpdate(hql);
	}
	
	public void unlockUser(String userName) {
		String hql = "update User set locked = " + Constants.USER_UNLOCKED + " where userName = '" + userName + "'";
		this.executeUpdate(hql);
	}
	
	// Add credit, update last visit time and IP address while sign in success.     
	public void upateUserAfterSignIn(User user) {
		String hql = "update User set credit = ?, lastVisit = now(), lastIp = ? where userName = ?";
		Object[] values = {user.getCredit() + Constants.SIGNIN_CREDIT, user.getLastIp(), user.getUserName()};
		this.executeUpdate(hql, values);
	}
    
	// Update user credit of digest topic, here isDigestTopic stands for original digest topic or not  
	public void updateUserCreditOfDigestTopic(Integer userId, Integer isDigestTopic) {
		if (isDigestTopic == Constants.DIGEST_YES) {
			String hql = "update User set credit = credit - " + Constants.DIGEST_CREDIT + " where userId = " + userId;	
			this.executeUpdate(hql);
		} else if (isDigestTopic == Constants.DIGEST_NO) {
			String hql = "update User set credit = credit + " + Constants.DIGEST_CREDIT + " where userId = " + userId;	
			this.executeUpdate(hql);
		}
	}
	
	// Update user credit while emit or remove topic 
	public void updateUserCreditByEmitOrRemoveTopic(Integer userId, Integer emitOrRemoveTopic) {
		if (emitOrRemoveTopic == Constants.TOPIC_EMIT) {
			String hql = "update User set credit = credit + " + Constants.TOPIC_CREDIT + " where userId = " + userId;
		    this.executeUpdate(hql);
		} else if (emitOrRemoveTopic == Constants.TOPIC_REMOVE) {
			String hql = "update User set credit = credit - " + Constants.TOPIC_CREDIT + " where userId = " + userId;
		    this.executeUpdate(hql);
		}
	}
	
	// Update user credit while emit or remove post
	public void updateUserCreditByEmitOrRemovePost(Integer userId, Integer emitOrRemovePost) {
		if (emitOrRemovePost == Constants.POST_EMIT) {
			String hql = "update User set credit = credit + " + Constants.POST_CREDIT + " where userId = " + userId;
			this.executeUpdate(hql);
		} else if (emitOrRemovePost == Constants.POST_REMOVE) {
			String hql = "update User set credit = credit - " + Constants.POST_CREDIT + " where userId = " + userId;
			this.executeUpdate(hql);
		}
	}
	
}